<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	public function override(){
		header('Location: /not-found');
	}
	
	public function index()
	{
        $this->load->model('dpcmodel');
		$displayData['pTab'] = '/';
        $allCategories=$this->dpcmodel->getParentCategories();
        $displayData['allCategories']=$allCategories;
        $allLatestCoupons=$this->dpcmodel->getTypeCoupons('Cpn', 'Home', '20');
        $displayData['allLatestCoupons']=$allLatestCoupons;
        $allFeaturedCoupons=$this->dpcmodel->getTypeCoupons('FeaturedCpn', 'Home', '20');
        $displayData['allFeaturedCoupons']=$allFeaturedCoupons;
        $displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
        $displayData['randomReviews'] = $this->dpcmodel->getRandomReviews();
        $displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
        $displayData['homebannerarray'] = $this->dpcmodel->getAllHomeBanner();
        $pageArray[0]['metatitle'] = 'Find latest Discount Promo Codes, Coupons, Offers, Deals in India.';
        $pageArray[0]['metadesc'] = 'Save money on shopping, air tickets booking, hotel bookings by using latest discount promo codes, coupons, offers, deals';
        $pageArray[0]['metakey'] = 'discount, promo, codes, coupons, vouchers, offers, deals';
        $displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-home', $displayData);
	}
	
	public function notFound()
	{
		$this->load->model('dpcmodel');
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$allLatestCoupons=$this->dpcmodel->getTypeCoupons('Cpn');
		$displayData['allLatestCoupons']=$allLatestCoupons;
		$pageArray[0]['metatitle'] = 'Not found -  Discountpromocodes.in.';
		$pageArray[0]['metadesc'] = 'Not found -  Discountpromocodes.in';
		$pageArray[0]['metakey'] = 'Not found, page not found';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-404page', $displayData);
	}
	
	public function topCodes()
	{
        $this->load->model('dpcmodel');
        $topCouponsArray=$this->dpcmodel->getTypeCoupons('TopCpn');
        $displayData['topCouponsArray']=$topCouponsArray;
		$displayData['pTab'] = 'top-discount-promo-codes';
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$pageArray[0]['metatitle'] = 'Find latest Top Promo Codes, Coupons, Offers, Deals in India.';
		$pageArray[0]['metadesc'] = 'Top Promo Codes, Top Coupons, Top Vouchers, Top Deals and Offers in India';
		$pageArray[0]['metakey'] = 'top promo codes, top discounts, top coupons, top deals, top offers, top voucher codes.';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-top-codes', $displayData);
	}
	
	public function topDeals()
	{
		$displayData['pTab'] = 'top-deals';
        $this->load->model('dpcmodel');
        $topDealsArray=$this->dpcmodel->getTopDeals();
        $displayData['topDealsArray']=$topDealsArray;
        $displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
        $displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
        $displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
        $pageArray[0]['metatitle'] = 'Get latest Top Deals, Offers, Discounts, Vouchers in India.';
        $pageArray[0]['metadesc'] = 'Get Latest Deals, Hot Offers, latest deals, Top Discount Prom codes, Top Voucher Codes, travel deals and enjoy shopping online.';
        $pageArray[0]['metakey'] = 'top deals, hot deals, top promo codes, top discounts, top coupons, top offers.';
        $displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-top-deals', $displayData);
	}
	
	public function aboutUs()
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = 'about-us';
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$pageArray[0]['metatitle'] = 'About Us -  Discountpromocodes.in.';
		$pageArray[0]['metadesc'] = 'About Us -  Discountpromocodes.in';
		$pageArray[0]['metakey'] = 'about us';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-aboutus', $displayData);
	}
	
	public function privacyPolicy()
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = 'privacy-policy';
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$pageArray[0]['metatitle'] = 'Privacy Policy -  Discountpromocodes.in.';
		$pageArray[0]['metadesc'] = 'Privacy Policy -  Discountpromocodes.in';
		$pageArray[0]['metakey'] = 'privacy policy';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-privacypolicy', $displayData);
	}
	
	public function contactUs()
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = 'contact-us';
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$pageArray[0]['metatitle'] = 'Contact Us -  Discountpromocodes.in.';
		$pageArray[0]['metadesc'] = 'Contact Us -  Discountpromocodes.in';
		$pageArray[0]['metakey'] = 'contact us';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-contactus', $displayData);
	}
	
	public function contact()
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = 'contact-us';
		$this->dpcmodel->insertContact($_POST);
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$displayData['sucess'] = 'Thankyou for mailng us. One of team member will get back to you soon.';
		$to = 'contact@techdreamssoftwares.com';
		$subject = 'Mail from DPC - '.$_POST['subject'];
		$message = $_POST['message'];
		$from = $_POST['email'];
		$headers = "From:" . $from;
		mail($to,$subject,$message,$headers);
		$pageArray[0]['metatitle'] = 'Contact Us -  Discountpromocodes.in.';
		$pageArray[0]['metadesc'] = 'Contact Us -  Discountpromocodes.in';
		$pageArray[0]['metakey'] = 'contact us';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-contactus', $displayData);
	}
	
	public function allStores()
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = 'all-promo-codes';
		$displayData['allStoresArray']=$this->dpcmodel->getAllStore();
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$pageArray[0]['metatitle'] = 'Discount Promo Codes - Coupon Codes, Discount Coupons of all stores.';
		$pageArray[0]['metadesc'] = 'Find Discount Promo Codes, Coupon Codes, Discount Coupons, Voucher Codes, Deals and Offers for your all favourite online stores and save money while shopping.';
		$pageArray[0]['metakey'] = 'all stores, all stores promo codes, coupon codes of all stores, all stores discount coupons, all stores promotional codes';
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-all-store', $displayData);
	}
	
	public function alphaStores($alpha)
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = $alpha;
		$displayData['allStoresArray']=$this->dpcmodel->getAlphaStores($alpha);
		$displayData['bannersArray']=$this->dpcmodel->getRandomBanners();
		$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
		$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$displayData['alpha'] = $alpha;
		$pageArray[0]['metatitle'] = 'Find Discount Promo Codes of all stores starts with alphabet '.$alpha;
		$pageArray[0]['metadesc'] = 'Search all stores starts with alphabet '.$alpha.' i.e. alphabetically. Find Promo Codes, Coupon Codes, Discount Coupons, Promotional Codes of all stores starts with alphabet '.$alpha;
		$pageArray[0]['metakey'] = 'search stores alphabetically, alphabetical stores search, search promo codes of all stores starts with alphabet '.$alpha;
		$displayData['pageArray'] = $pageArray;
		$this->load->view('dpc-alpha-stores', $displayData);
	}
	
	public function storePage($storeUrl)
	{
		$displayData['pTab'] = '';
        $this->load->model('dpcmodel');
        $storeDetailsArray=$this->dpcmodel->getStoreDetails($storeUrl);
        if(isset($storeDetailsArray) && !empty($storeDetailsArray)){
	        $displayData['storeDetailsArray']=$storeDetailsArray;
	        $ipAddr = $_SERVER["REMOTE_ADDR"];
	        $storeId=$storeDetailsArray[0]['store_id'];
            $storeName=$storeDetailsArray[0]['store_name'];
	        $displayData['store_id'] = $storeId;
	        $catId=$storeDetailsArray[0]['store_cat_id'];
	        $storeCouponsArray=$this->dpcmodel->getStoreCoupons($storeId);
	        $displayData['storeCouponsArray']=$storeCouponsArray;
	        $displayData['ratingsArray'] = $this->dpcmodel->rateYourEndure($storeId, 0, $ipAddr);
	        if(isset($displayData['ratingsArray'][0]['count']) && $displayData['ratingsArray'][0]['count']>'0'){
	        	$displayData['rated'] = '1';
	        }else{
	        	$displayData['rated'] = '0';
	        }
	        $displayData['relatedStore'] = $this->dpcmodel->getRelatedStores($storeId, $catId);
	        $displayData['relatedCat'] = $this->dpcmodel->getRelatedCategories($storeId, $catId);
	        $displayData['bannersArray']=$this->dpcmodel->getStoreBanners($storeId);
	        $displayData['expiredStoreCouponsArray']=$this->dpcmodel->getTypeCoupons('ExpiredCpn', $storeUrl);
	        $displayData['reviewsArray']=$this->dpcmodel->getStoreReviews($storeId);
	        $displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
	        $pageArray[0]['metatitle'] = $storeDetailsArray[0]['metatitle'];
		$pageArray[0]['metatitle'] = "$storeName Promo Codes, Discount Coupons, Offers, Deals";
	        $pageArray[0]['metadesc'] = $storeDetailsArray[0]['metadescription'];
	        $pageArray[0]['metakey'] = $storeDetailsArray[0]['metakeyword'];
	        $displayData['pageArray'] = $pageArray;
			$this->load->view('dpc-store-page', $displayData);
        }else{
        	header('Location: /not-found');
        }
	}
	
	public function rateYourEndure($storeId, $ratingStar){
		$this->load->model('dpcmodel');
		$ipAddr = $_SERVER["REMOTE_ADDR"];
		$storeDetailsArray = $this->dpcmodel->getStoreDetailsById($storeId);
		$displayData['storeName'] = $storeDetailsArray[0]['store_name'];
		$displayData['store_id'] = $storeId;
		$displayData['rated'] = '1';
		$displayData['ratingsArray'] = $this->dpcmodel->rateYourEndure($storeId, $ratingStar, $ipAddr);
		$this->load->view('common/star-rating', $displayData);
	}
	
	public function categoryPage($url)
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = '';
		$categoryArray = $this->dpcmodel->getParentCatByUrl($url);
		if(isset($categoryArray) && !empty($categoryArray)){
			$displayData['categoryArray'] = $categoryArray;
			$catId = $categoryArray[0]['parent_id'];
			$childCatArray = $this->dpcmodel->getCatByPId($catId);
			$displayData['childCatArray'] = $childCatArray;
			
			$couponCount = $this->dpcmodel->getParentCatCoupons($catId, '', '');
			$displayData['couponCount'] = $couponCount;
			$displayData['cur'] = '1';
			$displayData['next'] = '1';
			$start = '0';
			$limit = '5';
			$catCouponArray = $this->dpcmodel->getParentCatCoupons($catId, $start, $limit);
			$displayData['catCouponArray'] = $catCouponArray;
			if(count($couponCount)<5){
				$count = 5-count($couponCount);
				$catOfferArray = $this->dpcmodel->getParentCatOffers($catId, $count);
				$displayData['catOfferArray'] = $catOfferArray;
			}
			$displayData['otherCat'] = $this->dpcmodel->getOtherParent($catId);
			$displayData['catBanners'] = $this->dpcmodel->getCatRandomBanner($catId);
			$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
			$pageArray[0]['metatitle'] = $categoryArray[0]['parent_name'].' Promo Codes - Discount Coupons of all online '.strtolower($categoryArray[0]['parent_name']).' stores';
			$pageArray[0]['metadesc'] = $categoryArray[0]['parent_name'].' Discount Promo Codes - Find Promo Codes, Discount Coupons, Promotional Codes of all online '.$categoryArray[0]['parent_name'].' stores in India and save your money.';
			$pageArray[0]['metakey'] = strtolower($categoryArray[0]['parent_name']).', '.strtolower($categoryArray[0]['parent_name']).' promo codes, '.strtolower($categoryArray[0]['parent_name']).' coupon codes, '.strtolower($categoryArray[0]['parent_name']).' discount coupons, '.strtolower($categoryArray[0]['parent_name']).' promotional codes';
			$displayData['pageArray'] = $pageArray;
			$this->load->view('dpc-category-page', $displayData);
		}else{
			header('Location: /not-found');
		}
	}
	
	public function subCategoryPage($pUrl, $cUrl)
	{
		$this->load->model('dpcmodel');
		$displayData['pTab'] = '';
		$categoryArray = $this->dpcmodel->getSubCatByUrl($cUrl);
		if(isset($categoryArray) && !empty($categoryArray)){
			$displayData['categoryArray'] = $categoryArray;
			$catId = $categoryArray[0]['category_id'];
			$childCatArray = $this->dpcmodel->getCategoryStores($catId);
			$displayData['childCatArray'] = $childCatArray;
			$limit = '5';
			$displayData['catCouponArray']=$this->dpcmodel->getTopCategoryCoupons($catId, 0, $limit);
			$count = count($displayData['catCouponArray']);
			if($count<5){
				$limit = 5-$count;
				$displayData['catOfferArray']=$this->dpcmodel->getTopCategoryOffers($catId, 0, $limit);
			}
			$displayData['relatedCat'] = $this->dpcmodel->getAllCategories();
			$displayData['catBanners'] = $this->dpcmodel->getCatRandomBanner($catId);
			$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
			$pageArray[0]['metatitle'] = $categoryArray[0]['metatitle'];
			$pageArray[0]['metakey'] = $categoryArray[0]['metakeyword'];
			$pageArray[0]['metadesc'] = $categoryArray[0]['metadescription'];
			$displayData['pageArray'] = $pageArray;
			$this->load->view('dpc-subcat-page', $displayData);
		}else{
			header('Location: /not-found');
		}
	}
	
	public function loadCatCpn($val, $url)
	{
		$displayData['pTab'] = '';
		$this->load->model('dpcmodel');
		$categoryArray = $this->dpcmodel->getParentCatByUrl($url);
		$displayData['categoryArray'] = $categoryArray;
		$catId = $categoryArray[0]['parent_id'];
		$couponCount = $this->dpcmodel->getParentCatCoupons($catId, '', '');
		$displayData['couponCount'] = $couponCount;
		$displayData['cur'] = $val+1;
		$displayData['prev'] = $val-1;
		$displayData['next'] = $val+1;
			$start = 5*$val;
			$limit = '5';
			$catCouponArray = $this->dpcmodel->getParentCatCoupons($catId, $start, $limit);
			$displayData['catCouponArray'] = $catCouponArray;
			$displayData['popStoreList'] = $this->dpcmodel->getPopStoreList();
		$this->load->view('common/dpc-cat-cpn', $displayData);
	}
	
	public function redirect($coupon_id, $store_id){
		$this->load->model('dpcmodel');
		$displayData['affURL'] = $this->dpcmodel->getAffiliateURLByCouponId($coupon_id, $store_id);
		$this->load->view('waitingpage', $displayData);
	}
	
	public function dealsRedirect($coupon_id, $store_id){
		$this->load->model('dpcmodel');
		$displayData['affURL'] = $this->dpcmodel->getAffiliateURLByDealId($coupon_id, $store_id);
		$this->load->view('deals', $displayData);
	}
	
	public function searchStore($tag)
	{
		$this->load->model('dpcmodel');
		$tag=str_replace("%20"," ",$tag);
		$displayData['$tag']=$tag;
		$storeUrl = $this->dpcmodel->searchStore($tag);
		if(isset($storeUrl) && !empty($storeUrl)){
			$sUrl = $storeUrl[0]['store_url'];
			if($sUrl!=''){
				header('Location: /'.$sUrl.'-promo-codes');
			}
			else{
				header('Location: /');
			}
		}
		else{
			header('Location: /');
		}
	}
	
	public function subscribeEmail()
	{
		error_log('u r eher'.print_r($_GET, true));
		$this->load->model('dpcmodel');
		$subscribeId=$this->dpcmodel->subscribeEmailId($_GET);
		return $subscribeId;
	}
	
	public function generateSiteMap()
	{
		$this->load->model('dpcmodel');
		$this->load->model('enterprisesmodel');
	
		$pTabSelection='sitemap.xml';
		$displayData['pTabSelection']=$pTabSelection;
			
		$store = $this->dpcmodel->getAllStores();
		$category = $this->dpcmodel->getAllCategories();
		$parent = $this->dpcmodel->getParentCategories();
		
		$xmlBuild = '';
		$xmlBuild .= '<?xml version="1.0" encoding="UTF-8"?>';
		$xmlBuild .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/all-promo-codes</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/about-us.html</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/contact-us.html</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/privacy-policy.html</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/top-discount-promo-codes</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/top-deals</loc></url>';
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/all-categories</loc></url>';
			
		foreach($store as $rows => $values){
			$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/'.$values['store_url'].'-promo-codes</loc></url>';
		}
		foreach($parent as $rows => $values){
			$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/'.$values['parent_url'].'</loc></url>';
		}
		foreach($category as $rows => $values){
			$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/'.$values['parent_url'].'/'.$values['category_url'].'</loc></url>';
		}
		$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/all-promo-codes/0-9</loc></url>';
		for($a=65;$a<=90;$a++){
			$xmlBuild .= '<url><loc>http://'.$_SERVER['HTTP_HOST'].'/all-promo-codes/'.chr($a).'</loc></url>';
		}
	
		$xmlBuild .= '</urlset>';
		$myFile="sitemap.xml";
		$fh = fopen($myFile, 'w') or die("can't open file");
		fwrite($fh, $xmlBuild);
		fclose($fh);
	
		$displayData['msg'] = 'Your Sitemap.xml has been refreshed..';
		$this->load->view('sitemap', $displayData);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
